package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.provider.service.SemesterService;
import com.tfjybj.itoo.teach.FacadeApi.SemesterApi;
import com.tfjybj.itoo.teach.model.SemesterModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * SemesterController
 * semester表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-17 11:17:35
 */
@Api(tags = {"semester表接口"})
@RequestMapping(value = "/semester")
@RestController
public class SemesterController {

    @Resource
    private SemesterService semesterService;

    @Reference(check = false)
    private SemesterApi semesterApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model SemesterModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody SemesterModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        return semesterApi.create(model);
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        return semesterApi.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ItooResult.build(ItooResult.FAIL, "ids为空");
        }
        return semesterApi.deleteByIds(ids);
    }

    /**
     * 修改
     *
     * @param model SemesterModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "根据id修改semester")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody SemesterModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        return semesterApi.modify(model);
    }

    /**
     * 根据id查找Semester
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "ids为空");
        }
        return semesterApi.findById(id);
    }

    /**
     * 分页查询所有Semester
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "分页查询所有Semester")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        return semesterApi.queryPageAll(pageNo, pageSize);
    }

    /**
     * 根据学年学期名称模糊查询semester
     *
     * @param name     学年学期名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的semester
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "根据学年学期名称模糊查询semester", notes = "分页根据学年学期名称模糊查询semester")
    @GetMapping(value = "querySemesterByLikeName/{pageNo}/{pageSize}")
    public ItooResult querySemesterByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                              @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                              @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
       return semesterApi.querySemesterByLikeName(name, pageNo, pageSize);

    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 无参查找Semester
     *
     * @return semesterEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @ApiOperation(value = "无参查询semester表当前学年学期")
    @GetMapping(value = {"/selSemesterId"})
    public ItooResult selSemesterId() {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterService.selSemesterId());
    }
}
